package com.extension.company.view.home.search;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.extension.company.admin.R;
import com.extension.company.common.mvp.BaseActivity;
import com.extension.company.common.utils.ToastUtil;
import com.extension.company.contract.home.SearchContract;
import com.extension.company.dao.daoUtil.DBOperationUntil;
import com.extension.company.greenDaoDb.SearchHistoryDb;
import com.extension.company.presenter.home.SearchPresenter;
import com.extension.company.common.widget.dialog.DialogOK;
import com.extension.company.common.widget.flowLayout.FlowLayout;
import com.extension.company.common.widget.flowLayout.TagAdapter;
import com.extension.company.common.widget.flowLayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * create zwk
 * data   2019/2/19
 * 描述：fragmentHome中“搜索”模块
 */
public class SearchActivity extends BaseActivity<SearchContract.View, SearchPresenter> implements SearchContract.View, View.OnClickListener, TagFlowLayout.OnTagClickListener {

	@BindView(R.id.iv_back)
	ImageView iv_back;
	@BindView(R.id.iv_delete)
	ImageView iv_delete;
	@BindView(R.id.tv_search)
	TextView tv_search;
	@BindView(R.id.search_content)
	EditText searchContent;
	@BindView(R.id.fl_remen)
	TagFlowLayout fl_remen;
	@BindView(R.id.fl_lishi)
	TagFlowLayout historyTagFlowLayout;


	//	private ImageView iv_back, iv_delete;
//	private TextView tv_search;

	//	private TagFlowLayout fl_remen, historyTagFlowLayout;
	private DialogOK dialogOK;
	private List<String> valus;
	private List<String> histValues;
	//	private EditText searchContent;
	private TagAdapter<String> historyTagAdapter;
	private DBOperationUntil dbOperationUntil;

	public static void startSearchActivity(Activity activity) {
		Intent intent = new Intent(activity, SearchActivity.class);
		activity.startActivity(intent);
	}

	@Override
	public int getMainLayout() {
		return R.layout.search;
	}

	@Override
	public SearchContract.View createView() {
		return this;
	}

	@Override
	public SearchPresenter createPresenter() {
		return new SearchPresenter();
	}
//
//	@Override
//	protected void initView() {
//		iv_back = findViewById(R.id.iv_back);
//		iv_delete = findViewById(R.id.iv_delete);
//		tv_search = findViewById(R.id.tv_search);
//		fl_remen = findViewById(R.id.fl_remen);
//		searchContent = findViewById(R.id.search_content);
//		historyTagFlowLayout = findViewById(R.id.fl_lishi);
//	}

	@Override
	protected void initData() {
		dialogOK = new DialogOK(this);
		valus = new ArrayList<String>();
		valus.add("牛奶");
		valus.add("冰箱");
		valus.add("电风扇");
		valus.add("冰箱");
		valus.add("格力空调");
		valus.add("海尔冰箱");
		valus.add("热水器");
		TagAdapter<String> tagAdapter = new TagAdapter<String>(valus) {
			@Override
			public View getView(FlowLayout parent, int position, String s) {
				TextView tv_content = (TextView) LayoutInflater.from(SearchActivity.this).inflate(R.layout.search_tab_item,
						fl_remen, false);
				tv_content.setText(s);
				return tv_content;
			}
		};
		fl_remen.setAdapter(tagAdapter);

		// 历史记录

		histValues = new ArrayList<>();

		historyTagAdapter = new TagAdapter<String>(histValues) {
			@Override
			public View getView(FlowLayout parent, int position, String s) {
				TextView historyContent = (TextView) LayoutInflater.from(SearchActivity.this).inflate(R.layout.search_tab_item, historyTagFlowLayout, false);
				historyContent.setText(s);
				return historyContent;
			}
		};
		historyTagFlowLayout.setAdapter(historyTagAdapter);

		presenter.getAllDbList(this);

	}

	@Override
	protected void initListener() {
		iv_back.setOnClickListener(this);
		iv_delete.setOnClickListener(this);
		tv_search.setOnClickListener(this);
		fl_remen.setOnTagClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.iv_back:
				finish();
				break;
			case R.id.iv_delete:
				dialogOK.setContent("确定清空搜索记录吗？");
				dialogOK.setDialogOKListener(new DialogOK.DialogOKListener() {
					@Override
					public void dialogOKListenerOK() {
						presenter.deleteAll(SearchActivity.this);
					}

					@Override
					public void dialogOKListenerCancel() {
						dialogOK.dismiss();
					}
				});
				dialogOK.show();
				break;
			case R.id.tv_search:

				String content = searchContent.getText().toString().trim();
				presenter.insertDb(this, content);
				break;
			default:
				break;
		}
	}

	@Override
	public boolean onTagClick(View view, int position, FlowLayout parent) {
//		Toast.makeText(SearchActivity.this, valus.get(position), Toast.LENGTH_SHORT).show();
		SearchResultActivity.intent(SearchActivity.this);
		return true;
	}


	@Override
	public void showLoading() {

	}

	@Override
	public void hideLoading() {

	}

	@Override
	public void onError(Throwable throwable) {

	}

	@Override
	public <T> void searchAllDataSuccess(List<T> t) {

		for (int i = 0; i < t.size(); i++) {
			((SearchHistoryDb) t.get(i)).getId();
			Log.d("zwk", "id号是" + ((SearchHistoryDb) t.get(i)).getId());
			histValues.add(((SearchHistoryDb) t.get(i)).getName());
		}
		historyTagAdapter.notifyDataChanged();

	}

	@Override
	public void insertDataSuccess() {
		ToastUtil.showToast(this, "插入成功");
	}

	@Override
	public void deleteAllDataSuccess(boolean isTrue) {
		// 删除搜索数据成功
		if (isTrue) {
			histValues.clear();
			historyTagAdapter.notifyDataChanged();
			ToastUtil.showToast(this, "删除历史记录成功");
		}

	}
}
